<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <meta name="generator" content="Hugo 0.98.0" />

  
  <meta name="description" content="走在通往幸福的路上">
  

  
  <link rel="apple-touch-icon" sizes="180x180" href="https://blog.v5u.win/apple-touch-icon.png">

  
  <link rel="icon" type="image/png" sizes="32x32" href="https://blog.v5u.win/favicon-32x32.png">

  
  <link rel="icon" type="image/png" sizes="16x16" href="https://blog.v5u.win/favicon-16x16.png">

  
  <link rel="manifest" href="https://blog.v5u.win/site.webmanifest">

  
  <link rel="mask-icon" href="https://blog.v5u.win/safari-pinned-tab.svg" color="">

  <meta name="msapplication-TileColor" content="">

  <meta name="theme-color" content="">

  
  <link rel="stylesheet" href="https://blog.v5u.win/css/bootstrap.min.css" />

  
  <title>Go Toml | 为吾优</title>
  

  <style>
body {
  min-width: 300px;
}

.custom-navbar {
  margin-bottom: 1em;
  height: 60px;
}

.custom-navbar a {
  display: inline-block; 
  padding: 18px 0;
  margin-right: 1em; 
  font-weight: bold; 
}

.custom-navbar a:hover,
.custom-navbar a:focus {
  text-decoration: none; 
}

@media print {
  .custom-navbar {
    display: none;
  }
}

article {
  padding-bottom: 1em;
}

img {
  max-width: 100%;
}


body {
  background-color: #fff;
}



body {
  color: #212529;
}



a {
  color: #007bff;
}



a:hover,
a:focus {
  color: #0056b3;
}



.custom-navbar {
  background-color: #212529;
}



.custom-navbar a {
  color: rgba(255,255,255,.75);
}



.custom-navbar a:hover,
.custom-navbar a:focus {
  color: rgba(255,255,255,1);
}



.container {
  max-width: 800px;
}





</style>
</head>

<body>
  <nav class="custom-navbar">
  <div class="container">
    
    <a href="/">文章</a>
    
    <a href="/tags/">标签</a>
    
    <a href="/about/">关于</a>
    
    <a href="/index.xml">RSS</a>
    
  </div>
</nav>
  
  <div class="container">
    <article>
      <h1>Go Toml</h1>
<p>[TOC]</p>
<p>常用的配置文件有：ini, ymal, json, toml</p>
<p>说一下为什么要选择 toml。toml 格式是最新公布的配置文件格式，由GitHub创始人 Tom Preston-Werner 发明。TOML 的目标是成为一个极简的配置文件格式。TOML 被设计成可以无歧义地被映射为哈希表，从而被多种语言解析。</p>
<h3 id="toml的优势">TOML的优势</h3>
<ul>
<li>可以添加注释</li>
<li>没有缩进要求</li>
<li>表达简洁，丰富。</li>
<li>大小写敏感</li>
</ul>
<h3 id="写法-configtoml">写法 config.toml</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-toml" data-lang="toml"><span style="display:flex;"><span>[<span style="color:#a6e22e">app</span>]
</span></span><span style="display:flex;"><span>  <span style="color:#75715e">#app名称</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">name</span> = <span style="color:#e6db74">&#34;project-web&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">url</span> = <span style="color:#e6db74">&#34;http://localhost&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">port</span> = <span style="color:#e6db74">&#34;:8080&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">debug</span> = <span style="color:#66d9ef">false</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[<span style="color:#a6e22e">database</span>]
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">dirver</span> = <span style="color:#e6db74">&#34;mysql&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[<span style="color:#a6e22e">mysql</span>]
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">dbname</span> = <span style="color:#e6db74">&#34;@tcp(127.0.0.1:3306)/superstar?charset=utf8&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">username</span> = <span style="color:#e6db74">&#34;root&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">password</span> = <span style="color:#e6db74">&#34;333&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[<span style="color:#a6e22e">website</span>]
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">static_uri</span> = <span style="color:#e6db74">&#34;/static&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">site_title</span> = <span style="color:#e6db74">&#34;后台管理&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">copy_right</span> = <span style="color:#e6db74">&#34;&lt;small&gt;&amp;copy;2019&lt;/small&gt;&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[<span style="color:#a6e22e">image</span>]
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">image_lib</span> = <span style="color:#e6db74">&#34;Imagick&#34;</span> <span style="color:#75715e"># GD || Imagick</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">image_path</span>   = <span style="color:#e6db74">&#34;../app_images/&#34;</span> <span style="color:#75715e">#// 目录可读写</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">image_url</span>    = <span style="color:#e6db74">&#34;image/&#34;</span> <span style="color:#75715e">#// http://static.xxx.com/image/car_photo/150x150/19/sdddddd</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">image_org</span>    = <span style="color:#e6db74">&#39;org&#39;</span> <span style="color:#75715e">#// 原图路径名</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">image_tmp</span>    = <span style="color:#e6db74">&#34;tmp&#34;</span> <span style="color:#75715e">#// 临时路径名</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">image_types</span>  = [<span style="color:#e6db74">&#34;jpg&#34;</span>,<span style="color:#e6db74">&#34;jpeg&#34;</span>,<span style="color:#e6db74">&#34;png&#34;</span>,<span style="color:#e6db74">&#34;gif&#34;</span>]
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">water_mark</span>   = <span style="color:#e6db74">&#34;&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#75715e">#// 汽车品牌LOGO</span>
</span></span><span style="display:flex;"><span>  [[<span style="color:#a6e22e">image</span>.<span style="color:#a6e22e">image_categroy</span>]]
</span></span><span style="display:flex;"><span>    [[<span style="color:#a6e22e">image_categroy</span>.<span style="color:#a6e22e">car_logo</span>]]
</span></span><span style="display:flex;"><span>      <span style="color:#75715e">#// 哈希路径</span>
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">paths</span> = <span style="color:#e6db74">&#34;/car_logo/org/&#34;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e">#// 支持的尺寸</span>
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">sizes</span> = [<span style="color:#e6db74">&#34;100x100&#34;</span>,<span style="color:#e6db74">&#34;200x200&#34;</span>]
</span></span><span style="display:flex;"><span>	
</span></span></code></pre></div><h3 id="解析">解析</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">config</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> <span style="color:#e6db74">&#34;github.com/BurntSushi/toml&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Config</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">App</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">Name</span>  <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">URL</span>   <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">Port</span>  <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">Debug</span> <span style="color:#66d9ef">bool</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Database</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">Dirver</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>	} <span style="color:#e6db74">`toml:&#34;database&#34;`</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Mysql</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">Dbname</span>   <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">Username</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">Password</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>	} <span style="color:#e6db74">`toml:&#34;mysql&#34;`</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Website</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">static_uri</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">site_title</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">copy_right</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Image</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">ImageLib</span>   <span style="color:#66d9ef">string</span>   <span style="color:#e6db74">`toml:&#34;image_lib&#34;`</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">ImagePath</span>  <span style="color:#66d9ef">string</span>   <span style="color:#e6db74">`toml:&#34;image_path&#34;`</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">ImageURL</span>   <span style="color:#66d9ef">string</span>   <span style="color:#e6db74">`toml:&#34;image_ur&#34;`</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">ImageOrg</span>   <span style="color:#66d9ef">string</span>   <span style="color:#e6db74">`toml:&#34;image_org&#34;`</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">ImageTmp</span>   <span style="color:#66d9ef">string</span>   <span style="color:#e6db74">`toml:&#34;image_tmp&#34;`</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">ImageTypes</span> []<span style="color:#66d9ef">string</span> <span style="color:#e6db74">`toml:&#34;image_types&#34;`</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">WaterMark</span>  <span style="color:#66d9ef">string</span>   <span style="color:#e6db74">`toml:&#34;water_mark&#34;`</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">ImageCategory</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">CarLogo</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>				<span style="color:#a6e22e">Paths</span> <span style="color:#66d9ef">string</span>   <span style="color:#e6db74">`toml:&#34;paths&#34;`</span>
</span></span><span style="display:flex;"><span>				<span style="color:#a6e22e">Sizes</span> []<span style="color:#66d9ef">string</span> <span style="color:#e6db74">`toml:&#34;sizes&#34;`</span>
</span></span><span style="display:flex;"><span>			} <span style="color:#e6db74">`toml:&#34;carLogo&#34;`</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">ImgLogo</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>				<span style="color:#a6e22e">Paths</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`toml:&#34;paths&#34;`</span>
</span></span><span style="display:flex;"><span>			} <span style="color:#e6db74">`toml:&#34;imgLogo&#34;`</span>
</span></span><span style="display:flex;"><span>		} <span style="color:#e6db74">`toml:&#34;imageCategory&#34;`</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 解析类初始化
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">conf</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Config</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">AppConfig</span>() <span style="color:#f92672">*</span><span style="color:#a6e22e">Config</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">conf</span> <span style="color:#f92672">==</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">conf</span> = new(<span style="color:#a6e22e">Config</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">file</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;../config/config.toml&#34;</span>
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">toml</span>.<span style="color:#a6e22e">DecodeFile</span>(<span style="color:#a6e22e">file</span>, <span style="color:#a6e22e">conf</span>)
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Toml Error!&#34;</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Error</span>())
</span></span><span style="display:flex;"><span>		}
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">conf</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div>
    </article>
  </div>

  
  
  

  
</body>

</html>